package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.trading.operate.parameter.Order;
import com.ymatou.iapi.trading.operate.parameter.OrderInfoPo;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.ValidateRefundV2Req;
import com.ymatou.iapi.trading.operate.service.ValidateRefundV2Call;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.CancelOrderTypeEnum;
import com.ymttest.database.sharding.db.sharding.model.RefundBillPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;

/**
 * 退货退款申请效验接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ValidateRefundV2 {

	private static ValidateRefundV2Req validateRefundReq;
	private static ValidateRefundV2Call applyvalidationCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退货退款申请校验接口");
	}

	@Before
	public void caseUp() {
		validateRefundReq = new ValidateRefundV2Req();
		applyvalidationCall = new ValidateRefundV2Call();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(applyvalidationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 计算商品最大可退金额
	 * @param po
	 * @return
	 */
	public static BigDecimal getOrderDetailPaidAmountExcludeFreight(
			OrderInfoPo po, int productCount) {

		BigDecimal refundAmount = Utils.zeroIfNull(po.getProductPrice())
				.multiply(new BigDecimal(po.getAmount()))
				.add(Utils.zeroIfNull(po.getDiscount()))
				.subtract(Utils.zeroIfNull(po.getYmtCouponAmount()))
				.subtract(Utils.zeroIfNull(po.getSellerCouponAmount()))
				.subtract(Utils.zeroIfNull(po.getThirdPartyDiscount()));

		if (po.getAmount() != productCount) {

			refundAmount = refundAmount
					.divide(new BigDecimal(po.getAmount()),
							MathContext.DECIMAL32)
					.setScale(2, RoundingMode.DOWN)
					.multiply(new BigDecimal(productCount));

		}

		return refundAmount;

	}

	/**
	 * 计算商品最大可退运费
	 * @param po
	 * @return
	 */
	public static BigDecimal getMaxCanRefundFreight(Order order,
			BigDecimal productFreight) {

		long orderId = order.getOrderId();

		if (BigDecimal.ZERO.compareTo(productFreight) == 0) {

			return BigDecimal.ZERO;

		}

		List<RefundBillPo> res = orderShardingWapper
				.selectRefundBillListByOrderId(orderId);

		BigDecimal freight = res
				.stream()
				.filter(r -> r.getRefundBillStatus() == 1
						|| r.getRefundBillStatus() == 10)
				.map(f -> f.getFreight())
				.reduce(BigDecimal.ZERO, BigDecimal::add);

		return order.getFreight().subtract(freight);

	}
	/**
	 * 
	 * @param order
	 * @param orderDetailPo
	 * @param refundCount
	 */
	private void verify(Order order, OrderInfoPo orderDetailPo, int refundCount) {

		BigDecimal maxCanRefundAmount = getOrderDetailPaidAmountExcludeFreight(
				orderDetailPo, refundCount);

		BigDecimal maxCanRefundFreight = getMaxCanRefundFreight(order,
				orderDetailPo.getFreight());

		Logger.verifyEquals("true", applyvalidationCall.getString("success"),
				"验证返回码");

		DataUtil.verifyDecimalEquals(maxCanRefundAmount, new BigDecimal(
				applyvalidationCall.getString("maxCanRefundAmount")),
				"验证maxCanRefundAmount");

		DataUtil.verifyDecimalEquals(maxCanRefundFreight, new BigDecimal(
				applyvalidationCall.getString("maxCanRefundFreight")),
				"验证maxCanRefundFreight");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_001() {
		Logger.start(true, "用户下单后申请效验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderInfoList().get(0).getCatalogId());

			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);

			validateRefundReq.setApplyRefundCount(1);
			validateRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("申请退款金额不能大于实际可退款金额", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_002() {
		Logger.start(true, "用户支付后接单前效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = PlaceOrderService.payOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			validateRefundReq.setApplyRefundCount(1);
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderInfoList().get(0).getCatalogId());

			validateRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_CONDITION_NOTMATCH",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("当前订单状态不允许发起退款[%s]", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_003() {
		Logger.start(true, "商家接单前取消订单,效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = PlaceOrderService
					.cancelOrderAfterPaid(CancelOrderTypeEnum.BuyerCancelOrder);

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			validateRefundReq.setApplyRefundCount(1);
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderInfoList().get(0).getCatalogId());

			validateRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_ORDER_CANCELLED",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("订单%s已取消，不能退货退款", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_004() {
		Logger.start(true, "商家接单后,退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = PlaceOrderService.acceptOrder();

			Order order = placeOrderResp.getOrderList().get(0);

			Integer orderId = order.getOrderId();

			Logger.comment("前置数据准备结束");

			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			validateRefundReq.setApplyRefundCount(1);
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(order.getOrderInfoList().get(0)
					.getCatalogId());

			validateRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(order, order.getOrderInfoList().get(0),
					validateRefundReq.getApplyRefundCount());

		} catch (Exception e) {

			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ValidateRefundV2_009() {
		Logger.start(false, "订单号不存在申请效验");
		try {

			validateRefundReq.setOrderId(11111);
			validateRefundReq.setCatalogId("123");
			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			validateRefundReq.setApplyRefundCount(1);

			validateRefundReq.setRefundBillNo(String.format("TK%s", 11111));

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ErrOrderNotExists",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s不存在", validateRefundReq.getOrderId()),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ValidateRefundV2_010() {
		Logger.start(false, "订单号为null申请效验");
		try {
			// validateRefundReq.setOrderId(111111);
			validateRefundReq.setCatalogId("123");
			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			validateRefundReq.setApplyRefundCount(1);

			validateRefundReq.setRefundBillNo(String.format("TK%s", 11111));

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("orderId:订单号未提供",
							validateRefundReq.getOrderId()),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_014() {
		Logger.start(true, "商家发货后,退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			Order order = placeOrderResp.getOrderList().get(0);

			Integer orderId = order.getOrderId();
			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 发货
			orderCallService.dispatched(orderId, sellerId);

			Logger.comment("前置数据准备结束");
			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setRefundBillNo("TK0" + orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderInfoList().get(0).getCatalogId());

			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			validateRefundReq.setApplyRefundCount(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(order, order.getOrderInfoList().get(0),
					validateRefundReq.getApplyRefundCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_015() {
		Logger.start(true, "确认收货15天后,退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 发货
			orderCallService.dispatched(orderId, sellerId);
			// 确认收货
			orderCallService.confirmReceive(userId, orderId);
			new DefaultWapper().updateOrderConfirmedTimeByOrderId(orderId);
			Logger.comment("前置数据准备结束");
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setRefundBillNo("TK0" + orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderInfoList().get(0).getCatalogId());

			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			validateRefundReq.setApplyRefundCount(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_CONDITION_NOTMATCH",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s不允许退款，收货后360小时之内才允许", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_019() {

		Logger.start(true, "订单商品不是包邮,商品退款及退超过商品的运费，但不超过订单总运费");

		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			//第一个订单下的第一个商品
			OrderInfoPo orderInfoPo = order.getOrderInfoList().get(0);

			String catalogId = orderInfoPo.getCatalogId();

			String productId = orderInfoPo.getProductId();

			int productNum = orderInfoPo.getAmount();

			BigDecimal productFreight = orderInfoPo.getFreight();

			BigDecimal refundProductFreight = productFreight
					.add(new BigDecimal(2));

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setApplyRefundAmount(refundProductFreight
					.add(refoundAmout));

			validateRefundReq.setApplyRefundCount(orderInfoPo.getAmount());

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");
			Logger.verifyEquals("null", applyvalidationCall.getString("msg"),
					"验证msg");

			DataUtil.verifyDecimalEquals(refundProductFreight, BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_020() {

		Logger.start(true, "退款商品数量大于实际数量");

		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			//第一个订单下的第一个商品
			OrderInfoPo orderInfoPo = order.getOrderInfoList().get(0);

			BigDecimal productFreight = orderInfoPo.getFreight();

			BigDecimal refundProductFreight = productFreight
					.add(new BigDecimal(2));

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setRefundBillNo("sssssss");

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setApplyRefundAmount(refundProductFreight);

			validateRefundReq.setApplyRefundCount(orderInfoPo.getAmount() + 1);

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_CONDITION_NOTMATCH",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("申请退商品数量不能大于实际商品数量", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_021() {

		Logger.start(true, "订单商品不是包邮,申请单个商品退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			//第一个订单下的第一个商品
			OrderInfoPo orderInfoPo = order.getOrderInfoList().get(0);

			int productNum = orderInfoPo.getAmount();

			BigDecimal productFreight = orderInfoPo.getFreight();

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额

			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setApplyRefundCount(1);

			validateRefundReq.setApplyRefundAmount(oneProRefund);

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");
			Logger.verifyEquals("null", applyvalidationCall.getString("msg"),
					"验证msg");

			DataUtil.verifyDecimalEquals(BigDecimal.ZERO, BigDecimal
					.valueOf((Integer) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_022() {

		Logger.start(true, "订单商品不是包邮,申请单个商品退款加部分商品运费");

		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			//第一个订单下的第一个商品
			OrderInfoPo orderInfoPo = order.getOrderInfoList().get(0);

			String catalogId = orderInfoPo.getCatalogId();

			String productId = orderInfoPo.getProductId();

			int productNum = orderInfoPo.getAmount();

			BigDecimal productFreight = orderInfoPo.getFreight();

			BigDecimal refundProductFreight = productFreight
					.subtract(new BigDecimal(2));

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额

			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setApplyRefundCount(1);

			validateRefundReq.setApplyRefundAmount(oneProRefund
					.add(refundProductFreight));

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");
			Logger.verifyEquals("null", applyvalidationCall.getString("msg"),
					"验证msg");

			DataUtil.verifyDecimalEquals(refundProductFreight, BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_024() {

		Logger.start(true, "订单商品不是包邮,申请单个商品退款加订单总运费");

		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal orderFreight = order.getFreight();

			int orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			//第一个订单下的第一个商品
			OrderInfoPo orderInfoPo = order.getOrderInfoList().get(0);

			int productNum = orderInfoPo.getAmount();

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额

			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setApplyRefundCount(1);

			validateRefundReq.setApplyRefundAmount(oneProRefund
					.add(orderFreight));

			validateRefundReq.setRefundBillNo("xxxxx");

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			verify(order, order.getOrderInfoList().get(0),
					validateRefundReq.getApplyRefundCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_025() {

		Logger.start(true, "订单商品不是包邮,申请单个商品退款加上超过订单总运费");

		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal orderFreight = order.getFreight();

			int orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			//第一个订单下的第一个商品
			OrderInfoPo orderInfoPo = order.getOrderInfoList().get(0);

			int productNum = orderInfoPo.getAmount();

			BigDecimal refundProductFreight = orderFreight
					.add(new BigDecimal(2));

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额

			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setApplyRefundCount(1);

			validateRefundReq.setApplyRefundAmount(oneProRefund
					.add(refundProductFreight));

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("201", applyvalidationCall.getString("code"),
					"验证返回码");
			Logger.verifyEquals(
					String.format("OrderId:%s, 申请退运费金额不能大于实际可退运费金额", orderId),
					applyvalidationCall.getString("msg"), "验证msg");

			DataUtil.verifyDecimalEquals(BigDecimal.ZERO, BigDecimal
					.valueOf((Integer) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefundV2_026() {
		Logger.start(true, "订单锁定后申请效验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			new OrderCallService().markOrderLock(orderId, 1);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setRefundBillNo("ssssss");

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderInfoList().get(0).getCatalogId());

			validateRefundReq.setApplyRefundCount(1);

			validateRefundReq.setApplyRefundAmount(BigDecimal.ONE);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("该订单已被客服锁定，暂时无法对该订单进行后续操作", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
